package templatev01;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import physicshandler.*;
import physicshandler.Vector;

public class Handler implements KeyListener 
{

	private int SCALAR_MULTIPLIER = 5;
	Thread animationThread;
	Thread drawThread; 
	Screen screen;
	boolean run = true;
	double prevTime, curTime;
	double prevXDist, curXDist, prevYDist, curYDist;
	public Screen getScreen() {
		return screen;
	}

	public void setScreen(Screen screen) {
		this.screen = screen;
	}

	ArrayList<BasicShape> testShape = new ArrayList<BasicShape>();
	ArrayList<BasicShape> deadShape = new ArrayList<BasicShape>();
	// common display modes(more will be added for greater resolution types)/////////
	private static final DisplayMode possibleModes[] = {
		new DisplayMode(1360, 768, 32, 0),
		new DisplayMode(800, 600, 32, 0),
		new DisplayMode(800, 600, 24, 0),
		new DisplayMode(800, 600, 16, 0),
		new DisplayMode(640, 480, 32, 0),
		new DisplayMode(640, 480, 24, 0),
		new DisplayMode(640, 480, 16, 0),
		
	};
	///////////////////////////////////////////////////////////////////////////
	
	public void Run()
	{

		screen = new Screen();
		try
		{
			DisplayMode displayMode = screen.FindCompatibleMode(possibleModes); // this is where we find our screen
			screen.setFullScreen(displayMode);
			Inititialize();
			GameLoop(); // Coded in here is everything that changes(logic, redrawing, etc...)
		}
		finally 
		{
			screen.RestoreScreen();
		}
		
	}
	
	public void GameLoop()
	{
	
		while (run) // For now we can manually tell the game to stop using esc key or something
		{

			Graphics2D graphics = screen.getGraphics();
			if (testShape.size() > 0)
			{
				Animate();
			}
			Draw(graphics);
			graphics.dispose(); // garbage
			screen.update();
			
			try 
			{
				Thread.sleep(1);
			} catch (Exception exception) {}
		}
	}
		
	
	public void Draw(Graphics2D graphics)
	{
//			.. Draws images defined by a board or environment class	
		graphics.clearRect(0, 0, screen.getWidth(), screen.getHeight()); // Needed to clear previous screen
		
		for(int i = 0; i < testShape.size(); i++)
			testShape.get(i).Draw(graphics);
		
		for(int i = 0; i < deadShape.size(); i++)
		{
			deadShape.get(i).Draw(graphics);
		}
		
		graphics.drawString("Alive: " + testShape.size(), 50, 50);
		graphics.drawString("Dead : " + deadShape.size(), 50, 63);
			
	}
	
	
	public void Inititialize() 
	{
		// .. This is where I'd imagine we load images(sprites) and set any static values
		Window window = screen.getFullScreenWindow();
		addMouse(window);
		window.addKeyListener(this);
	
	}	
	
	public void Animate()
	{
	
		for(int i = 0; i < testShape.size(); i++)
		{
			if(	BasicShape.testDeath(testShape.get(i)) )
			{
				deadShape.add(testShape.get(i));
				testShape.remove(i);
			
			}
			else
				testShape.get(i).specificAnimation();
		}
		
		System.out.println("Moving     Object: " + testShape.size());
		System.out.println("Stationary Object: " + deadShape.size());
		
		
	}
	
	public void stop() { run = false; }

	@Override
	public void keyPressed(KeyEvent event) 
	{
		// TODO Auto-generated method stub
		int key = event.getKeyCode();
		if (key == KeyEvent.VK_ESCAPE)
			stop();
	
			
		
	}

	@Override
	public void keyReleased(KeyEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent event) {
		// TODO Auto-generated method stub
		
	}
	
	private void addMouse(Window window) 
	{
		window.addMouseListener(new MouseAdapter()
		{
			Vector mouseVector = new Vector();
			public void mousePressed(MouseEvent mouseEvent)
			{
			
				currentValues(mouseEvent);
				testShape.add(new BasicShape("Circle", mouseEvent.getX(), mouseEvent.getY(), 20, findMouseVector()));
				previousValues(mouseEvent);
				
			}
			
			private void currentValues(MouseEvent mouseEvent)
			{
				curTime = System.currentTimeMillis();
				curXDist = mouseEvent.getX();
				curYDist = mouseEvent.getY();
			}
			
			private void previousValues(MouseEvent mouseEvent)
			{
				prevTime = System.currentTimeMillis();
				prevXDist = mouseEvent.getX();
				prevYDist = mouseEvent.getY();
			}
			
			private Vector findMouseVector()
			{
				mouseVector.setCoordinates(((curXDist - prevXDist) * SCALAR_MULTIPLIER) / (curTime - prevTime),
						                   ((curYDist - prevYDist) * SCALAR_MULTIPLIER) / (curTime - prevTime));
				
				return mouseVector;
			}

	     });
	}


}
